<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属工厂" prop="factoryName">
        <el-input
          v-model="queryParams.factoryName"
          placeholder="请输入所属工厂"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入仓库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['vqms:storehouse:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['vqms:storehouse:delete']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="storehouseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所属工厂" align="center" prop="factoryName" :show-overflow-tooltip="true" width="300"/>
      <el-table-column label="仓库名称" align="center" prop="name" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="联系人" align="center" prop="contactName" :show-overflow-tooltip="true" width="150"/>
      <el-table-column label="联系电话" align="center" prop="contactPhone" :show-overflow-tooltip="true" width="150"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleLocationList(scope.row)"
            v-hasPermi="['vqms:storehouseLocation:query']"
          >库位管理</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['vqms:storehouse:update']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['vqms:storehouse:delete']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仓库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12" >
            <el-form-item label="所属工厂" prop="factoryName">
              <el-autocomplete
                clearable
                style="width:100%;"
                v-model="form.factoryName"
                value-key="name"
                :fetch-suggestions="querySearchAsync"
                placeholder="请输入所属工厂"
                @select="handleSelectFactory">
              </el-autocomplete>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓库名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入仓库名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人" prop="contactName">
              <el-input v-model="form.contactName" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人电话" prop="contactPhone">
              <el-input v-model="form.contactPhone" placeholder="请输入联系人电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 仓库库位列表对话框 -->
    <el-dialog :title="title2" :visible.sync="open2" width="1300px" append-to-body>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAddLocation"
            v-hasPermi="['vqms:storehouseLocation:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDeleteLocation"
            v-hasPermi="['vqms:storehouseLocation:delete']"
          >删除</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="storehouseLocationList" @selection-change="handleSelectionChangeLocation">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="所属工厂" align="center" prop="factoryName" :show-overflow-tooltip="true" width="200"/>
        <el-table-column label="所属仓库" align="center" prop="storehouseName" :show-overflow-tooltip="true" width="200"/>
        <el-table-column label="库位名称" align="center" prop="name" :show-overflow-tooltip="true" width="150"/>
        <el-table-column label="库位备注" align="center" prop="remark" :show-overflow-tooltip="true" width="150"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdateLocation(scope.row)"
              v-hasPermi="['vqms:storehouseLocation:update']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDeleteLocation(scope.row)"
              v-hasPermi="['vqms:storehouseLocation:delete']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total2>0"
        :total="total2"
        :page.sync="queryParams2.pageNum"
        :limit.sync="queryParams2.pageSize"
        @pagination="getStorehouseLocationList"
      />
    </el-dialog>

    <!-- 添加修改仓库库位对话框 -->
    <el-dialog :title="title3" :visible.sync="open3" width="1000px" append-to-body>
      <el-form ref="form2" :model="form2" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="库位名称" prop="name">
              <el-input v-model="form2.name" placeholder="请输入库位名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="库位备注" prop="remark">
              <el-input v-model="form2.remark" placeholder="请输入库位备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormLocation">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { selectFactoryList } from "@/api/vqms/factory";
import { listStorehouse,addStorehouse,getStorehouse,updateStorehouse,delStorehouse,listStorehouseLocation,addStorehouseLocation,getStorehouseLocation,updateStorehouseLocation,delStorehouseLocation } from "@/api/vqms/storehouse";

export default {
  //name: "Storehouse",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 仓库库位遮罩层
      loading2: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库库位总条数
      total2: 0,
      // 仓库表格数据
      storehouseList: [],
      // 仓库库位表格数据
      storehouseLocationList:[],
      // 工厂集合
      factoryList:[],
      // 工厂id
      factoryId:undefined,
      // 仓库id
      storehouseId:undefined,
      // 弹出层标题
      title: "",
      // 库位列表弹出层标题
      title2: "",
      // 库位新增、修改弹出层标题
      title3: "",
      // 是否显示弹出层
      open: false,
      // 库位列表是否显示弹出层
      open2: false,
      // 库位新增、修改是否显示弹出层
      open3: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createBy: undefined,
        status: undefined
      },
      // 仓库库位查询参数
      queryParams2: {
        storehouseId:undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 仓库库位表单参数
      form2: {},
      // 表单校验
      rules: {
        factoryName: [
          { required: true, message: "所属工厂不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询仓库列表 */
    getList() {
      this.loading = true;
      listStorehouse(this.queryParams).then(response => {
        this.storehouseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open3 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.form2 = {};
      this.resetForm("form");
      this.resetForm("form2");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加仓库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getStorehouse(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仓库";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateStorehouse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStorehouse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids
      this.$modal.confirm('是否确认删除？').then(function() {
        return delStorehouse(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 查询仓库库位列表 */
    async getStorehouseLocationList() {
      this.loading2 = true;
      this.reset();
      this.queryParams2.storehouseId=this.storehouseId
      await listStorehouseLocation(this.queryParams2).then(response => {
        this.storehouseLocationList = response.rows;
        this.total2 = response.total;
        this.loading2 = false;
      });
    },
     /** 库位管理按钮操作 */
     async handleLocationList(row) {
      this.queryParams2.pageNum = 1;
      this.storehouseId = row.id //赋值后，方便后续操作
      this.factoryId = row.factoryId
      await this.getStorehouseLocationList()
      this.open2 = true;
      this.title = "库位列表";
    },
    // 仓库库位多选框选中数据
    handleSelectionChangeLocation(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增仓库库位按钮操作 */
    handleAddLocation() {
      this.reset();
      this.open3 = true;
      this.title3 = "添加仓库库位";
    },
    /** 修改仓库库位按钮操作 */
    handleUpdateLocation(row) {
      this.reset();
      getStorehouseLocation(row.id).then(response => {
        this.form2 = response.data;
        this.open3 = true;
        this.title = "修改仓库库位";
      });
    },
    /** 提交仓库库位按钮 */
    submitFormLocation: function() {
      this.$refs["form2"].validate(valid => {
        if (valid) {
          if (this.form2.id != undefined) {
            updateStorehouseLocation(this.form2).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open3 = false;
              this.getStorehouseLocationList();
            });
          } else {
            this.form2.factoryId = this.factoryId
            this.form2.storehouseId = this.storehouseId
            addStorehouseLocation(this.form2).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open3 = false;
              this.getStorehouseLocationList();
            });
          }
        }
      });
    },
    /** 删除仓库库位按钮操作 */
    handleDeleteLocation(row) {
      const id = row.id || this.ids
      this.$modal.confirm('是否确认删除？').then(function() {
        return delStorehouseLocation(id);
      }).then(() => {
        this.getStorehouseLocationList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    //所属工厂异步搜索
    async querySearchAsync(queryString, cb) {
      this.factoryList=[];
      let query={
        name : queryString
      }
      await selectFactoryList(query).then(response => {
        this.factoryList = response.data
      });
      var results = queryString ? this.factoryList.filter(this.createStateFilter(queryString)) : this.factoryList;
      clearTimeout(this.timeout);
      this.timeout = setTimeout(() => {
        cb(results);
      }, 1000 * Math.random());
    },
    createStateFilter(queryString) {
      return (state) => {
        return (state.name.indexOf(queryString) >= 0);
      };
    },
    handleSelectFactory(item) {
      this.form.factoryName=item.name
      this.form.factoryId=item.id
    },
  }
};
</script>
